home *** CD-ROM | disk | FTP | other *** search
- Path: inforamp.net!ts9-03
- From: rmorin@inforamp.net (Randy Charles Morin)
- Newsgroups: comp.lang.c++
- Subject: Re: Printing class names
- Date: Thu, 07 Mar 96 05:57:12 GMT
- Organization: MiddleWorld SoftWare
- Message-ID: <4hltri$6jg@sam.inforamp.net>
- References: <4hj95e$cou@hermes.is.co.za>
- NNTP-Posting-Host: ts9-03.tor.inforamp.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4hj95e$cou@hermes.is.co.za>, "W. Dicks" <wd@isis.co.za> wrote:
- >I have a class called VeryLongNum with a derived class
- >VeryLongEvenNum. I then create instances of both types and add
- >them to a linked list. Later I print the values of these
- >numbers using the linked list's print functionality. The
- >VeryLongNum has an operator<< defined to print to stdout. The
- >functionality that I would like to add to the number classes
- >is to be able to print the type of the number based on the
- >class when the value of the number is printed. How would I do
- >this?
-
- I would define a virual write class name member function for the parent and
- the child class. The operator<< can call this class. Example:
-
- virtual void VeryLongNum::WriteClassName(opstream& op)
- {
- op << "VeryLongNum";
- };
-
- virtual void VeryLongEvenNum::WriteClassName(opstream& op)
- {
- op << "VeryLongEvenNum";
- };
-
- friend opstream& operator << (opstream& ps, VeryLongNum& r)
- {
- ...
- r.WriteClassName(ps);
- ...
- };
-
- I hope that's what you are looking for.
-
- Agrivar
-